<?php
/**
 * 添加部门
 */
apf_require_controller("HrAbstract");

class Hr_Ajax_AddDepartmentController extends HrAbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $status=0;
        //检测参数合法性
        $this->_check_params($params);

        $dep_bll = new Bll_Hr_Department();
        $indata = array(
            'company_id'=>$params['company'],
            'city_id'=>$params['city'],
            'cost_city'=>$params['cost'],
            'function_id'=>$params['skill'],
            'bu_id'=>$params['buit'],
            'department_name'=>$params['depart'],
            'plan_num'=>intval($params['formation']),
            'created'=>time(),
        );
        //check 未被删除的同名部门
        $check_same = $dep_bll->check_same_department($indata);
        if($check_same){
            $this->json_encode($status,Const_Tip::ERROR_DEPARTMENT_EXISTS);
        }
        //search start
        $status = 1;
        $result = $dep_bll->add_department($indata);
        $this->json_encode($status,$result);
    }
    /**
     * 参数检测
     */
    private function _check_params($params){
        $company = $params['company'];
        $bu = $params['buit'];
        $skill = $params['skill'];
        $costcity = $params['cost'];
        $depart = $params['depart'];
        $params['formation'] = intval($params['formation']);
        $status=0;
        if(empty($depart) || empty($company) || empty($costcity) || empty($bu) || empty($skill)){
            $this->json_encode($status,Const_Tip::ERROR_PARAM);
        }
        if($params['formation'] < 0 || $params['formation'] >100000){
            $this->json_encode($status,Const_Tip::ERROR_DEPARTMENT_PLAN_NUM);
        }
        if(mb_strlen($depart,'UTF-8') >10){
            $this->json_encode($status,Const_Tip::ERROR_DEPARTMENT_LENGTH);
        }
        return true;
    }
}